﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Job.Logic.Common;
using Job.Data.Interfaces;
using Job.Entity;
using System.Data;
using Job.Web.Framework;

namespace Job.Logic.Personal
{
    public class SearcherLogic
    {
        public int GetSearcherAccount(int id, string code)
        {
            IJobPerson1SearcherDao dao = DataAccess.person1SearcherDao;
            return dao.GetCountByAccount(id, code);
        }

        //个人管理自己的个人搜索器（搜索企业）时显示的列表
        public DataTable GetPageSearcherByAccount(
               int personId, string loginCode, int pageSize, int pageIdx)
        {
            loginCode = (null == loginCode) ? "" : loginCode.Trim().Replace("'", "''");
            string sql = "Select searcher_id,searcher_name,search_key,account_status "
                + "From job_person_1_searcher "
                + "Where person_id = " + personId.ToString()
                + " And person_account = '" + loginCode + "'";

            CommonLogic common = new CommonLogic();
            DataSet ds = common.GetPageDataBySql(sql, pageSize, pageIdx, 0,
                "searcher_id", SortDirection.Descending, null);

            return ds.Tables[0];
        }

        public JobPerson1Searcher GetSearcher(int id)
        {
            IJobPerson1SearcherDao dao = DataAccess.person1SearcherDao;
            return dao.Find(id);
        }

        public void InsertSearcher(JobPerson1Searcher searcher)
        {
            IJobPerson1SearcherDao dao = DataAccess.person1SearcherDao;
            dao.Insert(searcher);
        }
        public void UpdateSearcher(JobPerson1Searcher searcher)
        {
            IJobPerson1SearcherDao dao = DataAccess.person1SearcherDao;
            dao.UpdateByAccount(searcher);
        }
        public void DeleteSearcher(JobPerson1Searcher searcher)
        {
            IJobPerson1SearcherDao dao = DataAccess.person1SearcherDao;
            dao.DeleteByAccount(searcher);
        }

        //根据定义的搜索器搜索企业资料
        public int CountCompanyBySearcher(JobPerson1Searcher searcher)
        {
            string sql = "Select * From job_company_info Where " + GetWhere(searcher);

            CommonLogic common = new CommonLogic();
            DataSet ds = common.GetPageDataBySql(sql, 10, 1, 1,
                "regist_date", SortDirection.Descending, null);
            return (Int32)ds.Tables[0].Rows[0][0];
        }

        public DataTable GetPageCompanyBySearcher(JobPerson1Searcher searcher,
            int pageSize, int pageIdx)
        {
            string sql = "Select company_info_id,company_name,regist_date,company_property,contact_man,"
                + "contact_phone From job_company_info Where " + GetWhere(searcher);

            CommonLogic common = new CommonLogic();
            DataSet ds = common.GetPageDataBySql(sql, pageSize, pageIdx, 0,
                "regist_date", SortDirection.Descending, null);

            return ds.Tables[0];
        }

        private string GetWhere(JobPerson1Searcher searcher)
        {
            StringBuilder buf = new StringBuilder();
            buf.Append(" 1=1 ");
            if ( searcher.CompanyName != null && searcher.CompanyName.Length > 1)
            {
                buf.Append(" And company_name like '%");
                buf.Append(searcher.CompanyName.Replace("'","''"));
                buf.Append("%' ");
            }
            if ( searcher.LoginName != null && searcher.LoginName.Length > 1)
            {
                buf.Append(" And login_name like '%");
                buf.Append(searcher.LoginName.Replace("'","''"));
                buf.Append("%' ");
            }
            
            if ( searcher.CompanyProperty > 0 ) 
                buf.Append(" And company_property=" + searcher.CompanyProperty.ToString());

            if ( searcher.CompanyIndustry != null && searcher.CompanyIndustry.Length >= 4)
            {
                buf.Append(" And company_industry = '" + searcher.CompanyIndustry + "' ");
            }
            else if ( searcher.CompanyIndustry != null && searcher.CompanyIndustry.Length > 1)
            {
                buf.Append(" And company_industry like = '" + searcher.CompanyIndustry + "%' ");
            }

            if ( searcher.CompanyCorporation != null && searcher.CompanyCorporation.Length > 0 )
            {
                buf.Append(" And company_corporation like '%");
                buf.Append(searcher.CompanyCorporation.Replace("'","''"));
                buf.Append("%' ");
            }
            if ( searcher.ContactMan != null && searcher.ContactMan.Length > 0 )
            {
                buf.Append(" And contact_man like '%");
                buf.Append(searcher.ContactMan.Replace("'", "''"));
                buf.Append("%' ");
            }
            if ( searcher.RegistDiff > 0 ) 
            {
                buf.Append(" And regist_date >= cast(cast(getdate() - ");
                buf.Append(searcher.RegistDiff.ToString());
                buf.Append(" as varchar(10)) as datetime) ");
            }
            if ( searcher.LoginDiff > 0 ) 
            {
                buf.Append(" And last_login_date >= cast(cast(getdate() - ");
                buf.Append(searcher.LoginDiff.ToString());
                buf.Append(" as varchar(10)) as datetime) ");
            }

            if ( searcher.CompanyScope > 0 ) 
            {
                buf.Append(" And company_scope = " + searcher.CompanyScope.ToString());
            }

            if ( searcher.CompanyAddr != null && searcher.CompanyAddr.Length >= 4 ) 
            {
                buf.Append(" And company_addr = '" + searcher.CompanyAddr + "'");
            }
            else if ( searcher.CompanyAddr != null && searcher.CompanyAddr.Length >= 1)
            {
                buf.Append(" And company_addr like '" + searcher.CompanyAddr + "%'");
            }

            if ( searcher.AccountStatus == 0 || searcher.AccountStatus == 1)
            {
                buf.Append(" And account_status=" + searcher.AccountStatus.ToString());
            }
            if (searcher.SearchKey != null && searcher.SearchKey.Length > 0 )
            {
                string key = searcher.SearchKey.Replace("'","''");
                buf.Append(" And (company_intro like '%");
                buf.Append(key);
                buf.Append("%' Or product_intro like '%");
                buf.Append(key);
                buf.Append("%') ");
            }

            return buf.ToString();
        }
    
    }
}
